package com.newture.lxz.services.impl;

import com.newture.lxz.constant.Constant;
import com.newture.lxz.db.mapper.LdOrderItemMapper;
import com.newture.lxz.db.model.LdOrderItem;
import com.newture.lxz.db.model.LdOrderItemExample;
import com.newture.lxz.errors.AppException;
import com.newture.lxz.errors.Errors;
import com.newture.lxz.pvg.Pvg;
import com.newture.lxz.services.IOrderItemSrv;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service("OrderItemSrv")
public class OrderItemSrv implements IOrderItemSrv {
    @Resource
    LdOrderItemMapper ldOrderItemMapper;

    @Override
    public int insert(LdOrderItem obj) {
        Short quanZ = new Short((short)0);
        obj.setCreator(Pvg.getPvgName());
        obj.setModifier(Pvg.getPvgName());
        obj.setGmtCreated(LocalDateTime.now());
        obj.setIsDeleted(Constant.NO);
        obj.setVersion(1);
        obj.setGmtModified(LocalDateTime.now());
        obj.setQuantity(quanZ);
        int effectCount = ldOrderItemMapper.insert(obj);
        if(effectCount != 1) {
            throw new AppException(Errors.SAVE_ERR.name(), obj.toString(), "OrderItemSrv.insert() failed");
        }

        return obj.getId();
    }

    @Override
    public LdOrderItem findById(int id, String[] columns) {
        LdOrderItemExample example = new LdOrderItemExample();
        LdOrderItemExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andIsDeletedEqualTo(Constant.NO);

        example.setDynamicColumns(columns);
        List<LdOrderItem> list = ldOrderItemMapper.selectByExample(example);
        if(list.isEmpty()) {
            throw new AppException(Errors.NO_RECORD_FOUND_BY_ID.name(),
                    String.format("ID is %s", id), "No record found in CourtAddressSrv.findById()");
        }

        if(list.size() > 1) {
            throw new AppException(Errors.MORE_THAN_ONE_RECORDS_FOUND.name(),
                    String.format("ID is %s", id), "More than one record found in CourtAddressSrv.findById()");
        }

        return list.get(0);
    }

    @Override
    public int insertSelective(LdOrderItem obj) {
        return 0;
    }
}
